package reflect;

import java.io.File;
import java.net.URISyntaxException;

public class Test2 {
    public static void main(String[] args) throws Exception{
//        String name = Test2.class.getPackage().getName();
//        File file = new File(Test2.class.getResource(".").toURI());
//        String[] list = file.list();
//        for (String s : list) {
//            String[] split = s.split("\\.");
//            Class<?> aClass = Class.forName(name+"." + split[0]);
//            Object o = aClass.newInstance();
//            System.out.println(o);
//        }
        File dir = new File(
                Test2.class.getResource(".").toURI()
        );
        String packname = Test2.class.getPackage().getName();
        System.out.println(packname);
        File[] subs = dir.listFiles(f->f.getName().endsWith(".class"));
        for(File sub : subs){
            String fileName = sub.getName();
            //根据文件名截取出类名
            String className = fileName.substring(0,fileName.indexOf("."));
            System.out.println(className);
            Class cls = Class.forName(packname+"."+className);//"reflect.Person"
            Object obj = cls.newInstance();

        }

    }
}
